﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class ThietBiController : Controller
    {
        //
        // GET: /Main/ThietBi/
        CommandSql cm = new CommandSql();
        CMSMVC4Entities db = new CMSMVC4Entities();

        [OutputCache(Duration = 900, VaryByParam = "id", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult Index(string id)
        {
            Category data;
            int i;
            if (int.TryParse(id, out i))
            {
                data = db.Categories.FirstOrDefault(x => x.Id == i);
            }
            else
            {
                data = db.Categories.OrderByDescending(x => x.Sort).FirstOrDefault(x => x.Type == MenuType.ThiteBi && x.Status == CategoriStatus.Thuong && x.paren == 0);
            }
            PageLang page = null;
            if (data != null)
            {
                string lg = cm.GetLanguage();
                var catel = db.CateLangs.FirstOrDefault(x => x.CategoriesId == data.Id && x.LanguageCode.Equals(lg));
                if (catel != null)
                {
                    ViewBag.Title = catel.Name;
                    ViewBag.Id = catel.CategoriesId;
                    ViewBag.key = catel.Name;
                }
                var pagelang = cm.GetPageLang(data.Id);
                if (pagelang.Rows.Count > 0)
                {
                    int idpageone = Convert.ToInt32(pagelang.Rows[0]["PageId"].ToString());
                    page = db.PageLangs.FirstOrDefault(x => x.PageId == idpageone);
                }
            }
            if (page != null)
            {
                ViewBag.IdPage = page.PageId;
                ViewBag.description = page.Summary;
            }
            //select menu dang chon
            var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.ThiteBi);
            if (menu != null)
            {
                ViewBag.Menu = menu.Id;
            }
            else
            {
                ViewBag.Menu = 0;
            }
            return View(page);
        }

        [OutputCache(Duration = 900, VaryByParam = "id", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult Mota(BaiVietModel model)
        {
            if (ModelState.IsValid)
            {
                var row = db.PageLangs.FirstOrDefault(x => x.PageId == model.id);
                if (row != null)
                {
                    string lg = cm.GetLanguage();
                    var catel = db.CateLangs.FirstOrDefault(x => x.CategoriesId == row.Page.CategoriesID && x.LanguageCode.Equals(lg));
                    if (catel != null)
                    {
                        ViewBag.Title = catel.Name;
                        ViewBag.key = catel.Name;
                        ViewBag.description = row.Summary;
                    }
                    ViewBag.IdPage = row.PageId;
                    ViewBag.Id = row.Page.CategoriesID;
                }
                //select menu dang chon
                var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.ThiteBi);
                if (menu != null)
                {
                    ViewBag.Menu = menu.Id;
                }
                else
                {
                    ViewBag.Menu = 0;
                }
                if (Request.IsAjaxRequest())
                {
                    return PartialView("IndexAjax",row);
                }
                return View("Index", row);
            }
            return View("Error");
        }

        [ChildActionOnly]
        public ActionResult ListTab(int id, string idpage)
        {
            var data = cm.GetListPageLag(id);
            ViewBag.IdPage = idpage;
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListService(string id)
        {
            var data = cm.GetNameCategories(MenuType.ThiteBi);
            ViewBag.id = id;
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListFoder(string id)
        {
            string lag = cm.GetLanguage();
            var data =
                db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(
                    x => x.Category.Type == MenuType.ThiteBi && x.Category.Status == CategoriStatus.Foder && x.LanguageCode.Equals(lag)).ToList();
            int paren = int.Parse(id);
            var cate = db.Categories.FirstOrDefault(x => x.Id == paren);
            if(cate != null)
            {
                ViewBag.paren = cate.paren;
            }else
            {
                ViewBag.paren = 0;
            }
            ViewBag.id = id;
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ShowThietBi(int id,int paren,bool show)
        {
            string lag = cm.GetLanguage();
            var data =
                db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(
                    x => x.Category.Type == MenuType.ThiteBi && x.Category.paren == paren && x.Category.Status == CategoriStatus.Thuong && x.LanguageCode.Equals(lag)).ToList();
            ViewBag.id = id;
            ViewBag.show = show;
            return PartialView(data);
        }
    }
}
